/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __SPHERE_H__
#define __SPHERE_H__

#include "Vector3.h"
#include "Matrix4.h"

namespace liba
{
namespace math
{

class Sphere
{
public:
	explicit Sphere( const Vector3<float> center = Vector3<float>( 0, 0, 0 ), float radius = 0 );
	bool intersect( const Sphere & sphere )const;
	bool contains( const Sphere & sphere )const;
	bool contains( const Vector3<float> & point )const;

	const Vector3<float> & get_center()const
	{
		return center;
	}
	float get_radius()const
	{
		return radius;
	}

	void transform( const Matrix4<float> & transform )
	{
		Matrix4<float> hre;//workaround internal compiler error
		center = transform * center;

		Matrix4<float> mat = Matrix4<float>::translate( -transform.shift() ) * transform;

		Vector3<float> vec( radius, 0, 0 );
		vec = mat * vec;

		float new_rad = vec.length();

		vec = Vector3<float>( 0, radius, 0 );
		vec = mat * vec;

		float length = vec.length();
		if ( length > new_rad )
			new_rad = vec.length();

		vec = Vector3<float>( 0, 0, radius );
		vec = mat * vec;

		length = vec.length();
		if ( length > new_rad )
			new_rad = vec.length();

		radius = new_rad;
	}

	void merge( const Sphere & sphere );
private:
	Vector3<float> center;
	float radius;
};

} // namespace math
} // namespace liba
using namespace liba;

#endif //__SPHERE_H__